package com.simon.hse.activity.education;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.CommonOutput;
import com.simon.hse.http.entity.ExperienceInput;
import com.simon.hse.http.entity.ExperienceOutput;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import retrofit2.Call;

/**
 * 培训心得录入
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_train_input)
public class TrainInputActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.et_train_input_content)
    EditText mEt_train_input_content;

    @ViewInject(R.id.et_train_input_experience)
    EditText mEt_train_input_experience;

    @ViewInject(R.id.et_train_input_suggest)
    EditText mEt_train_input_suggest;

    @ViewInject(R.id.ll_train_input_bottom)
    LinearLayout mLl_train_input_bottom;

    private String id;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        if (bundle == null) {
            return;
        }

        type = bundle.getInt("TYPE");
        id = bundle.getString("ID");
        if (type == 0) {
            mTv_common_title.setText("培训心得录入");
        } else {
            mTv_common_title.setText("培训心得详情");
            mLl_train_input_bottom.setVisibility(View.GONE);

            initExperienceDetail();
        }
    }

    @Event(value = {R.id.iv_common_left, R.id.et_train_input_cancel, R.id.et_train_input_confirm})
    private void back(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
            case R.id.et_train_input_cancel:
                finish();
                break;
            case R.id.et_train_input_confirm:
                submit();
                break;
        }
    }

    /**
     * 心得详情
     */
    private void initExperienceDetail() {
        Call<String> call = HttpManager.getInstance().requestService().EXPERIENCE_DETAIL(id);
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                ExperienceOutput experienceOutput = new Gson().fromJson(result, ExperienceOutput.class);
                if (experienceOutput.getResult().equals("Success")) {
                    final ExperienceOutput.DataBean dataBean = experienceOutput.getData();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mEt_train_input_content.setText(dataBean.getContent());
                            mEt_train_input_experience.setText(dataBean.getExperience());
                            mEt_train_input_suggest.setText(dataBean.getAdvice());
                            mEt_train_input_content.setFocusable(false);
                            mEt_train_input_experience.setFocusable(false);
                            mEt_train_input_suggest.setFocusable(false);
                        }
                    });
                } else {
                    ToastUtils.getInstance().showShortToastBottom(experienceOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

    /**
     * 提交
     */
    private void submit() {
        String content = mEt_train_input_content.getText().toString();
        String experience = mEt_train_input_experience.getText().toString();
        String suggest = mEt_train_input_suggest.getText().toString();

        if (TextUtils.isEmpty(content) && TextUtils.isEmpty(experience) && TextUtils.isEmpty(suggest)) {
            ToastUtils.getInstance().showShortToastBottom("请输入培训心得!");
            return;
        }

        ExperienceInput experienceInput = new ExperienceInput();
        experienceInput.setPlanID(id);
        experienceInput.setContent(content);
        experienceInput.setAdvice(suggest);
        experienceInput.setExperience(experience);

        Call<String> call = HttpManager.getInstance().requestService().EXPERIENCE_ADD(HttpManager.getInstance().requestBody(experienceInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                CommonOutput commonOutput  = new Gson().fromJson(result, CommonOutput.class);
                if (commonOutput.getResult().equals("Success")) {
                    ToastUtils.getInstance().showShortToastBottom("提交成功!");
//                        Intent intent = new Intent();
//                        Bundle bundle = new Bundle();
//                        bundle.putInt("SUCCESS", 1);
//                        intent.putExtras(bundle);
//                        setResult(RESULT_OK, intent);
                    finishActivity(TrainInputActivity.this);
                } else {
                    ToastUtils.getInstance().showShortToastBottom(commonOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }
}
